package com.wjm;

import com.wjm.until.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Data:2022年09月06日
 * Author:wjm
 * Content:   分页查询
 */
public class JdbcLimit {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的页数：");
        int page = scanner.nextInt();
        System.out.println("请输入一页的条数：");
        int pageSize = scanner.nextInt();


        try(Connection connection = JdbcUtil.getConnection();) {
            //sql
            String sql = "select * from users limit ?,?;";

            //创建语句对象
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            //给？赋值
            preparedStatement.setInt(1,(page-1)*pageSize);
            preparedStatement.setInt(2,pageSize);

            //执行语句
            ResultSet resultSet = preparedStatement.executeQuery();

            //获取数据
            while (resultSet.next()){
                String name = resultSet.getString("username");
                String pwd = resultSet.getString("password");
                System.out.println("用户名："+name+" 密码："+pwd);
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
